package com.hd.crdao.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;

import com.hd.crdao.R;
import com.hd.crdao.adapter.CarSourcePagerAdapter;
import com.hd.crdao.adapter.MyCarSourceAdapter;
import com.hd.crdao.fragment.MyDraftFragment;
import com.hd.crdao.fragment.MyRackingFragment;
import com.hd.crdao.fragment.MyStoreFragment;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.view.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 *@author Programming
 *@date 2017/5/22 0022
 */
public class MyCarSourceActivity extends FragmentActivity {


    @BindView(R.id.mycarsource_head)
    ExpandHeadView mMycarsourceHead;
    @BindView(R.id.pagersligingtabstrip)
    PagerSlidingTabStrip mPagersligingtabstrip;
    @BindView(R.id.pager)
    ViewPager mPager;

    private List<Fragment> mFragments = new ArrayList<>();
    public DisplayMetrics mDisplayMetrics;
    private final String[] titles = {"已上架", "仓库", "草稿"};
    public CarSourcePagerAdapter mCarSourcePagerAdapter;


    public MyRackingFragment mMyRackingFragment;
    public MyStoreFragment mMyStoreFragment;
    public MyDraftFragment mMyDraftFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycarsource);
        ButterKnife.bind(this);
        initData();
        initEvnent();
    }

    private void initData() {
        mDisplayMetrics = getResources().getDisplayMetrics();
        mMyRackingFragment = new MyRackingFragment();
        mMyStoreFragment = new MyStoreFragment();
        mMyDraftFragment = new MyDraftFragment();
        mFragments.add(0, mMyRackingFragment);
        mFragments.add(1, mMyStoreFragment);
        mFragments.add(2, mMyDraftFragment);


    }

    private void initEvnent() {
        mMycarsourceHead.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        mMycarsourceHead.getHeadViewRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Fragment fragment = mCarSourcePagerAdapter.getItem(mPager.getCurrentItem());
                if (fragment instanceof MyRackingFragment) {
                    mMyRackingFragment.AllSelected();
                    MyCarSourceAdapter myCarSourceAdapter = mMyRackingFragment.mMyCarSourceAdapter;
                    if(myCarSourceAdapter!=null){
                        myCarSourceAdapter.notifyDataSetChanged();
                    }

                } else if (fragment instanceof MyStoreFragment) {
                    mMyStoreFragment.AllSelected();
                    MyCarSourceAdapter myCarSourceAdapter = mMyStoreFragment.mMyCarSourceAdapter;
                    if(myCarSourceAdapter!=null){
                        myCarSourceAdapter.notifyDataSetChanged();
                    }
                } else {
                    mMyDraftFragment.AllSelected();
                    MyCarSourceAdapter myCarSourceAdapter = mMyDraftFragment.mMyCarSourceAdapter;
                    if(myCarSourceAdapter!=null){
                        myCarSourceAdapter .notifyDataSetChanged();
                    }
                }
            }
        });

        mCarSourcePagerAdapter = new CarSourcePagerAdapter(getSupportFragmentManager(), mFragments,titles);
        mPager.setAdapter(mCarSourcePagerAdapter);
        mPagersligingtabstrip.setViewPager(mPager);

        setTabValue();


    }

    /**
     * 设置tab的一些参数
     */
    private void setTabValue() {
        // 设置Tab是自动填充满屏幕的
        mPagersligingtabstrip.setShouldExpand(true);

        // 设置Tab的分割线是透明的
        mPagersligingtabstrip.setDividerColor(Color.TRANSPARENT);
        // 设置Tab底部线的高度
        mPagersligingtabstrip.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, mDisplayMetrics));
        // 设置Tab Indicator的高度
        mPagersligingtabstrip.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 2, mDisplayMetrics));
        // 设置Tab标题文字的大小
        mPagersligingtabstrip.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 14, mDisplayMetrics));
        // 设置Tab标题默认的颜色*
        mPagersligingtabstrip.setTextColor(getResources().getColor(
                R.color.get_record_text_unselected_color));
        // 设置选中Tab标题的颜色
        mPagersligingtabstrip.setSelectedTextColor(getResources().getColor(
                R.color.get_record_text_selected_color));
        // 设置Tab底部线的颜色
        mPagersligingtabstrip.setUnderlineColor(getResources().getColor(
                R.color.get_record_line_unselected_color));
        // 设置Tab Indicator的颜色
        mPagersligingtabstrip.setIndicatorColor(getResources().getColor(
                R.color.get_record_line_selected_color));
        // 取消点击Tab时的背景色
        // mPagersligingtabstrip.setTabBackground(getResources().getColor(R.color.tab_pressed_hover));

    }

}


